function [ma, Ca] = geom2arith(mg, Cg, t)
%GEOM2ARITH Geometric  to arithmetic moments of asset returns.
% Transform moments associated with a continuously-compounded geometric Brownian motion into
% equivalent moments associated with a simple Brownian motion with a possible change in periodicity.
%
%		[ma, Ca] = geom2arith(mg, Cg);
%		[ma, Ca] = geom2arith(mg, Cg, t);
%
% Inputs:
%	mg - Continuously-compounded or "geometric" mean of asset returns (positive n-vector).
%	Cg - Continuously-compounded or "geometric" covariance of asset returns (n x n symmetric
%		positive semidefinite-matrix).
%
% Optional Inputs:
%	t - Target period of arithmetic moments in terms of periodicity of geometric moments with
%		default value 1 (positive scalar).
%
% Outputs -
%	ma - Arithmetic mean of asset returns over the target period (n-vector).
%	Ca - Arithmetric covariance of asset returns over the target period (n x n matrix).
%
% Notes:
%	Geometric returns over period tg are modeled as multivariate lognormal random variables
%		Y ~ LN(1 + mg, Cg)
%	with moments E[Y] = 1 + mg and cov(Y) = Cg .
%
%	Arithmetic returns over period ta are modeled as multivariate normal random variables
%		X ~ N(ma, Ca)
%	with moments E[X] = ma and cov(X) = Ca .
%
%	The transformation from geometric to arithmetic moments is
%		Ca(i,j) = cov(X(i),X(j)) = t*log(1 + Cg(i,j)/(1 + mg(i))*(1 + mg(j)))
%		ma(i) = E[X(i)] = t*log(1 + mg(i)) -  0.5*Ca(i,i)
%	for i, j = 1, ... , n with t = ta/tg. Note that if t = 1, then X = log(Y).
%
%	This function requires that the input mean must satisfy 1 + mg > 0 and that the input covariance
%	Cg must be a symmetric positive-semidefinite matrix.
%
%	The functions geom2arith and arith2geom are complementary so that, given m, C, and t, the
%	sequence
%		[ma, Ca] = geom2arith(m, C, t);
%		[mg, Cg] = arith2geom(ma, Ca, 1/t);
%	yields mg = m and Cg = C.
%
% See also ARITH2GEOM.

%	Copyright 2007 The MathWorks, Inc.
%	$Revision: 1.1.6.2 $ $Date: 2007/06/04 21:07:38 $

%
% Examples:
%	Given geometric mean m and covariance C of monthly total returns, obtain annual arithmetic mean
%	ma and covariance Ca. In this case, the output period (1 year) is 12 times the input period
%	(1 month) so that t = 12 with
%		[ma, Ca] = geom2arith(m, C, 12);
%
%	Given annual geometric mean m and covariance C of asset returns, obtain monthly arithmetic
%	mean ma and covariance Ca. In this case, the output period (1 month) is 1/12 times the input
%	period (1 year) so that t = 1/12 with
%		[ma, Ca] = geom2arith(m, C, 1/12);
%
%	Given geometric means m and standard deviations s of daily total returns (derived from 260
%	business days per year), obtain annualized arithmetic mean ma and standard
%	deviations sa with
%		[ma, Ca] = geom2arith(m, diag(s .^2), 260);
%		sa = sqrt(diag(Ca));
%
%	Given geometric mean m and covariance C of monthly total returns, obtain quarterly
%	arithmetic return moments. In this case, the output is 3 of the input periods so
%	that t = 3 with
%		[ma, Ca] = geom2arith(m, C, 3);
%
%	Given geometric mean m and covariance C of 1254 observations of daily total returns over
%	a 5-year period, obtain annualized arithmetic return moments. Since the periodicity
%	of the geometric data is based on 1254 observations for a 5-year period, a 1-year period for
%	arithmetic returns implies a target period of t = 1254/5 so that
%		[ma, Ca] = geom2arith(m, C, 1254/5);

if nargin < 2
	error('Finance:geom2arith:MissingInputArgument', ...
		'A required input argument is missing.');
end
if nargin < 3 || isempty(t)
	t = 1;
end

if ~isvector(mg) || ~isa(mg,'double')
	error('Finance:geom2arith:InvalidInputArg', ...
		'Input mean should be a vector.');
end
if ndims(Cg) ~= 2 || ~isa(Cg,'double')
	error('Finance:geom2arith:InvalidInputArg', ...
		'Input covariance should be a matrix.');
end
if ~isscalar(t) || ~isa(t,'double')
	error('Finance:geom2arith:InvalidInputArg', ...
		'Input target period should be a scalar.');
end

if size(mg,1) < size(mg,2)
	flip = true;
	mg = mg(:);
else
	flip = false;
end

mg = 1 + mg;

if any(mg <= 0)
	error('Finance:geom2arith:InvalidGeometricMean', ...
		'Geometric mean must be positive.');
end	
if ~all(size(Cg) == size(mg,1))
	error('Finance:geom2arith:NonConformableInputs', ...
		'Non-conformable mean and covariance inputs.');
end
if norm(Cg - Cg',inf) > eps
	warning('Finance:geom2arith:AsymmetricCovariance', ...
		'Non-symmetric covariance input will be made symmetric.');
	Cg = 0.5*(Cg + Cg');
end
[L, D] = ldl(Cg);
if any(diag(D) < 0)
	error('Finance:geom2arith:InvalidCovariance', ...
		'Non-positive-semidefinite covariance input.');
end
if t < 0 || ~isfinite(t)
	error('Finance:geom2arith:InvalidPeriodicity', ...
		'Invalid relative periodicity of geometric returns.');
end

Ca = t*log(1 + Cg ./ (mg*mg'));
ma = t*log(mg) - 0.5*diag(Ca);

if flip
	ma = ma';
end
